﻿using Albatross.SSRSViewer.Configuration;
using Albatross.WPF.Common;
using Albatross.WPF.Common.ViewModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Albatross.SSRSViewer.ViewModels {
	public class ShellMenuViewModel : ViewModel {
		public RelayCommand<IViewService> OpenCommand { get { return new RelayCommand<IViewService>(Open); } }
		void Open(IViewService svcs) {
			if (DefaultServer != null) {
				svcs.ShowWorkspace<ReportViewerViewModel>(null, DefaultServer.Name);
			}
		}

		public RelayCommand<IViewService> SetupServersCommand { get { return new RelayCommand<IViewService>(SetupServers, CanSetupServers); } }
		void SetupServers(IViewService svcs) { }
		bool CanSetupServers(IViewService svcs) { return true; }

		public RelayCommand<IViewService> OptionsCommand { get { return new RelayCommand<IViewService>(Options, CanOptions); } }
		void Options(IViewService svcs) { }
		bool CanOptions(IViewService svcs) { return true; }

		public ServerCfgElement DefaultServer { get; private set; }

		public ShellMenuViewModel() {
			Servers = new ObservableCollection<ServerCfgElement>();
			RecentReports = new ObservableCollection<WorkspaceConstruct>();
			
			Load();
		}
		public ObservableCollection<ServerCfgElement> Servers { get; private set; }
		public ObservableCollection<WorkspaceConstruct> RecentReports { get; private set; }

		void Load() {
			foreach (ServerCfgElement cfgElem in Configuration.SSRSViewerCfgSection.GetSection(false).Servers) {
				if (cfgElem.Default) { DefaultServer = cfgElem; }
				Servers.Add(cfgElem);
			}
			if (DefaultServer == null) {
				DefaultServer = Servers.FirstOrDefault();
			}
		}
	}
}
